package com.borderbears.frontend
{
	import com.baseoneonline.flash.utils.logger.Logger;
	
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.getQualifiedClassName;
	
	public class Config
	{
		private static var instance:Config;
		
		private var log:Logger = Logger.getInstance(getQualifiedClassName(this));
		
		public static var data:XML;
		
		public function Config()
		{
		}
		
		public function load(file:String, callBack:Function):void {
			var ldr:URLLoader = new URLLoader();
			ldr.dataFormat = URLLoaderDataFormat.TEXT;
			ldr.addEventListener(Event.COMPLETE, function(e:Event):void {
				data = new XML(ldr.data);
				log.info("Config loaded.");
				if (null != callBack) {
					callBack();
				}
			}, false, 0, true);
			ldr.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void {
				log.fatal("Could not load config: "+file+"\n"+e.text);
			}, false, 0, true);
			ldr.addEventListener(HTTPStatusEvent.HTTP_STATUS, function(e:HTTPStatusEvent):void {
				// Unhandled
			}, false, 0, true);
			ldr.load(new URLRequest(file));
		}
		
		public function getBoolean(key:String):Boolean {
			return new Boolean(getString(key));
		}
		
		public function getString(key:String):String {
			var re:String = data[key];
			if (null == re || re.length == 0) {
				log.warn("Key not found: "+key);
			}
			return re;
		}
		
		public static function getConfig():Config {
			if (null == instance) {
				instance = new Config();
			}
			return instance;
		}

	}
}